package inimigos;

import org.anddev.andengine.audio.sound.SoundManager;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureManager;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

public class Rato extends Inimigo {

	private BitmapTextureAtlas mapaDeTexturas;
	private TiledTextureRegion textura;

	public Rato(int x, int y, Context contexto, TextureManager tm, SoundManager sm){
		this.x = x;
		this.y = y;
		xInicial = x;
		largura = 128;
		altura = 64;
		
		mapaDeTexturas = new BitmapTextureAtlas(256, 64, TextureOptions.BILINEAR);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/inimigos/");
		textura = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mapaDeTexturas, contexto, "Rato.png", 0, 0, 2, 1);
		tm.loadTexture(mapaDeTexturas);

		spriteAnimado = new AnimatedSprite(x, y, textura);
		spriteAnimado.animate(200, true);
	}

	@Override
	public void atualiza() {
		if(direcao == false){//esquerda
			x -= 4;
			if(x <= xInicial){
				direcao = true;
				spriteAnimado.getTextureRegion().setFlippedHorizontal(direcao);
			}
		}else{//direita
			x += 4;
			if(x >= xInicial + 192){
				direcao = false;
				spriteAnimado.getTextureRegion().setFlippedHorizontal(direcao);
			}
		}
	}

	@Override
	public void posicionaNaCena(int mapx, int mapy) {
		spriteAnimado.setPosition((int)x-mapx, (int)y-mapy);
	}

	@Override
	public boolean colidiuComCaoGuia(AnimatedSprite spriteCaoGuia) {
		if(spriteCaoGuia.collidesWith(spriteAnimado)){
			return true;
		}else{
			return false;
		}
	}

}
